package com.example.common.util.picutil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import javax.imageio.ImageIO;

public class ImageUtils {

    public static void main(String[] args) {
        String imgUrl = "https://n.sinaimg.cn/sinacn12/733/w1400h933/20180410/ab8a-fyvtmxe9278329.jpg";
        String outPath = "/Users/zjarlin/Desktop/test.png";
        addTextWaterMark(imgUrl, outPath, LocalDate.now().toString(), Position.BOTTOM_RIGHT);
    }

    /**
     * 添加文字水印
     *
     * @param imgUrl   img url
     * @param outPath  出路径
     * @param text     文本
     * @param position 位置枚举
     * @author zjarlin
     * @since 2023/03/22
     */
    public static void addTextWaterMark(String imgUrl, String outPath, String text, Position position) {
        try {
            URL url = new URL(imgUrl);
            BufferedImage image = ImageIO.read(url);

            Graphics2D graphics2D = image.createGraphics();
            graphics2D.setPaint(Color.RED);//水印文字的颜色
            graphics2D.setFont(new Font("微软雅黑", Font.PLAIN, 18));//设置字体以及字体大小，可以根据需要调整
            graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//抗锯齿
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f));//设置透明度

            int x = 0, y = 0;
            int margin = 40;//文字与图片边缘的距离
            switch (position) {
                case TOP_LEFT://左上
                    x = margin;
                    y = margin + graphics2D.getFontMetrics().getHeight();
                    break;
                case TOP_RIGHT://右上
                    x = image.getWidth() - graphics2D.getFontMetrics().stringWidth(text) - margin;
                    y = margin + graphics2D.getFontMetrics().getHeight();
                    break;
                case BOTTOM_LEFT://左下
                    x = margin;
                    y = image.getHeight() - margin;
                    break;
                case BOTTOM_RIGHT://右下
                    x = image.getWidth() - graphics2D.getFontMetrics().stringWidth(text) - margin;
                    y = image.getHeight() - margin;
                    break;
                default:
                    break;
            }
            graphics2D.drawString(text, x, y);//添加水印文本
            graphics2D.dispose();

            File outFile = new File(outPath);//输出文件路径
            ImageIO.write(image, "png", outFile);//输出文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 给网络图片加文字水印
     *
     * @param imgUrl  图片url
     * @param outPath 输出文件路径
     * @param text
     */
    public static void addTextWaterMark(String imgUrl, String outPath, final String text) {
        try {
            URL url = new URL(imgUrl);
            BufferedImage image = ImageIO.read(url);

            Graphics2D graphics2D = image.createGraphics();
            graphics2D.setPaint(Color.RED);//水印文字的颜色
            graphics2D.setFont(new Font("微软雅黑", Font.PLAIN, 18));//设置字体以及字体大小

            graphics2D.drawString(text, image.getWidth() - 500, image.getHeight() - 100);//绘制文本水印

            File outFile = new File(outPath);//输出文件路径
            ImageIO.write(image, "png", outFile);//输出文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public enum Position {
        TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
    }
}
